﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

    /// <summary>
    /// Summary description for rss.
    /// </summary>
public partial class rss : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable DT = Dal.ExecuteDBsp("Admin_GetRssNews");

        string strRss = "<?xml version='1.0' encoding='utf-8'?><rss version='2.0'><channel>";
        strRss += "<title>Новости сайта</title><link>http://Medserver.co.il</link><description>Наши последние новости</description><language>ru-RU</language>";
        if(DT!=null)
            if (DT.Rows.Count > 0)
            {
                foreach (DataRow DR in DT.Rows)
                {
                    try
                    {
                        strRss += string.Format("<item><title>{0}</title>", DR["Title"].ToString());
                        strRss += string.Format("<guid>{0}</guid>", GetLink(DR));
                        strRss += string.Format("<link>{0}</link>", GetLink(DR));
                        strRss += string.Format("<pubDate>{0}</pubDate>", DateTime.Parse(DR["PublishDate"].ToString()).AddHours(-3).ToString("r"));
                        strRss += string.Format("<description>{0}</description></item>", StripHtml(DR["SubTitle"].ToString().TrimEnd()));
                    }
                    catch { }
                    
                }
            }
        strRss += "</channel></rss>";
        Response.ContentType = "text/xml";
        
        Response.Write(strRss);
        Response.End();
    }

    private string StripHtml(string html)
    {
        if (html == null || html == string.Empty)
            return string.Empty;

        String sRetString = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
        sRetString = System.Text.RegularExpressions.Regex.Replace(sRetString, "&[^>]*;", string.Empty);
        //sRetString = sRetString.Replace("&nbsp;", "");
        //sRetString = sRetString.Replace("&ndash;", "");
        //sRetString = sRetString.Replace("&ldquo;", "");
        //sRetString = sRetString.Replace("&rdquo;", "");
        //sRetString = sRetString.Replace("&laquo;", "");
        //sRetString = sRetString.Replace("&raquo;", ""); 
        
        return sRetString;
    }

    private string GetLink(DataRow DR)
    {
        string sRetString = "http://medserver.co.il/";
        int iAncestorID = Convert.ToInt32(DR["ancestorID"]);
        NewsType newsType=NewsType.news;

        switch (iAncestorID)
        {
            case 3:
                newsType = NewsType.Wiki;
                break;
            case 4:
                newsType = NewsType.MedicalCenters;
                break;
            case 8:
                newsType = NewsType.hotels;
                break;
            default:
                int iPID = Convert.ToInt32(DR["PID"]);
                if (iPID == 3)
                    newsType = NewsType.Wiki;
                break;
        }


        switch (newsType)
        {
            case NewsType.news:
            case NewsType.popular:
                sRetString += string.Format("{0}-a.aspx", DR["cPageName"].ToString());
                break;
            case NewsType.deadSea:
                sRetString += string.Format("deadsea.aspx");
                break;
            case NewsType.aboutUS:
                sRetString += string.Format("about.aspx");
                break;
            case NewsType.Wiki:
                sRetString += string.Format("{0}-wa.aspx", DR["cPageName"].ToString());
                break;
            case NewsType.MedicalCenters:
                sRetString += string.Format("{0}-ca.aspx", DR["cPageName"].ToString());
                break;
            case NewsType.MedicalTourism:
                sRetString += string.Format("{0}-a.aspx", DR["cPageName"].ToString());
                break;
            case NewsType.hotels:
                sRetString += string.Format("{0}-a.aspx", DR["cPageName"].ToString());
                break;
        }
        return sRetString;
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.Load += new EventHandler(this.Page_Load);
    }
    #endregion
}


